//action
function ajax_action(mod,action,method,id){
	var setings = "";
	var obj = $.ajax({			
		async: false,
		type: "POST",
		url: "?mod="+mod+"&act="+action,
		data: "method="+method+"&uuid="+id,									
		success: function(res) {
			setings = res;
			return res;
		}
	});
	return setings;
}
function memberBlock(uuid){
	if($("#"+uuid).attr('class')=='error'){
		$("#"+uuid).removeClass('error');
		$("#block-"+uuid).removeClass("btn-warning").find('i').removeClass('icon-white');
		$("#status-"+uuid).html("active");
		ajax_action('member','ajax_member_action','unblocked',uuid);
	}else{
		$("#"+uuid).attr('class','error');
		$("#block-"+uuid).addClass("btn-warning").find('i').addClass('icon-white');
		$("#status-"+uuid).html("blocked");
		ajax_action('member','ajax_member_action','blocked',uuid);
	}
}
function categoryDeactivate(uuid){
	if($("#"+uuid).attr('class')=='warning'){
		$("#"+uuid).removeClass('warning');
		$("#inactive-"+uuid).removeClass("btn-warning").find('i').removeClass('icon-eye-close').addClass('icon-eye-open');
		$("#status-"+uuid).html("active");
		ajax_action('category','ajax_category_action','activate',uuid);
	}else{
		$("#"+uuid).attr('class','warning');
		$("#inactive-"+uuid).addClass("btn-warning").find('i').removeClass('icon-eye-open').addClass('icon-eye-close');
		$("#status-"+uuid).html("inactive");
		ajax_action('category','ajax_category_action','deactivate',uuid);
	}
}
function categoryDelete(uuid){
	if(sure()){
		$("#"+uuid).remove();
		$cnt = $(".id-delete").size()-1;
		if($cnt < 1){window.location.reload();}
		ajax_action('category','ajax_category_action','delete',uuid);
	}
}
function deleteKey(uuid){
	if(sure()){
		$("#"+uuid).remove();
		$cnt = $(".id-delete").size()-1;
		if($cnt < 1){window.location.reload();}
		ajax_action('keys','ajax_keys_action','delete',uuid);
	}
}
//Select all
function selectAll(name, checked, formName)
{
	var names = [];
	if (name.length == 0)
		names = ["p_id[]","c_id[]"];
	else 
		names = [name];
	for(var j =0; j < names.length; j++){
			name=names[j];
		var cb = document.getElementsByName(name);
		for (var i=0; i < cb.length; i++)
		{
			if (formName == null || cb[i].form.name == formName) {
				if (cb[i].disabled == false) {
					cb[i].checked = checked;
				}
			}
		}
	}
}
//sure?
function sure(){
	return confirm('Are you sure?');
}
//Select at least one item...
function isCheck(sure){
	var cb = document.getElementsByName("uuid[]");
	var ch = false;
	for(var i=0;i < cb.length; i++){
		if(cb[i].checked == true){ ch = true;}
	}
	if(ch){
		if(sure == 1){
			 return confirm('Are you sure?');
		}else{
			return true;
		}
	}
	else {alert('Please select at least one item from the list');return false;}
}
$(function() {
 	$( ".datepicker" ).datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat:"mm/dd/y"
	});
});
function add_member(){
	var login = $("#form-add-member-input-login").attr('value');
	var email = $("#form-add-member-input-email").attr('value');
	var pass = $("#pass").attr('value');
	var uuid = $("#form-input-uuid").attr('value');
	$.ajax({				
		type: "POST",
		async:false,
		url: "/admin/index.php?mod=member&act=ajax_add_member",			
		data: "data[login]="+login+"&data[email]="+email+"&data[pass]="+pass+"&data[uuid]="+uuid,
		dataType: "json",									
		success: function (res) {
			$("#form-error").html('');
			if(res.status == "error"){
				$("#form-error").html("<div class='alert alert-error fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
			}else if(res.status == "ok_update"){
				hideModal('add_member');
				$("#form-msg").html("<div class='alert alert-success fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
				$(".success").removeClass("success");
				$("#"+res.uuid).attr('class','success');
				$("#"+res.uuid).find(".login").html(res.login);
				$("#"+res.uuid).find(".email").html(res.email);
			}else if(res.status == "ok"){
				hideModal('add_member');
				$("#form-msg").html("<div class='alert alert-success fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");

				var tpl_tr = $('#tpl_tr').clone();
				$(tpl_tr).attr('id',res.uuid);
				$(tpl_tr).find('.login').html(res.login);
				$(tpl_tr).find('.email').html(res.email);
				$(tpl_tr).find('.id-block').attr('id',"block-"+res.uuid).attr('onclick',"memberBlock('"+res.uuid+"');");
				$(tpl_tr).find('.id-status').attr('id',"status-"+res.uuid);
				$(".success").removeClass("success");
				$(tpl_tr).attr('class','success');
				$('#tpl_tr').after($(tpl_tr));
			}
		}
	});
}
function add_member_key(){
	var member_uuid = $("#form-add-member-key-input-member-uuid").val();
	var category = $("#form-add-member_key-input-category").val();
	var title = $("#form-add-member_key-input-title").val();
	var protocol = $("#form-add-member-key-input-protocol").val();
	var host = $("#form-add-member-key-input-host").val();
	var port = $("#form-add-member-key-input-port").val();
	var login = $("#form-add-member-key-input-login").val();
	var password = $("#form-add-member-input-password").val();
	var comment = $("#form-add-member-input-comment").val();
	var uuid = $("#form-input-uuid").val();

	$.ajax({
		type: "POST",
		async:false,
		url: "/admin/index.php?mod=keys&act=ajax_add_key",
		data: {"data[member_uuid]":member_uuid, "data[category]":category, "data[title]":title, "data[protocol]":protocol, "data[host]":host, "data[port]":port, "data[login]":login, "data[password]": password, "data[comment]":comment, "data[uuid]":uuid},
		dataType: "json",
		success: function (res) {
			$("#form-error").html('');
			if(res.status == "error"){
				$("#form-error").html("<div class='alert alert-error fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
			}else if(res.status == "ok"){
				hideModal('add_member_key');
				$("#form-msg").html("<div class='alert alert-success fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
				
				var tpl_tr = $('#tpl_tr').clone();
				$(tpl_tr).attr('id',res.id);
				$(tpl_tr).find('.title').html(res.param_1);
				$(tpl_tr).find('.uuid_check').val(res.id);
				$(tpl_tr).find('.protocol').html(res.param_2);
				$(tpl_tr).find('.host').html(res.param_3);
				$(tpl_tr).find('.port').html(res.param_4);
				$(tpl_tr).find('.login').html(res.param_5);
				$(tpl_tr).find('.category').html(res.category_title);
				$(tpl_tr).find('.category').html(res.category_title);
				$(tpl_tr).find('.id-edit').attr('onclick','editKey("'+res.id+'","'+member_uuid+'")');
				$(tpl_tr).find('.id-delete').attr('onclick','delete("'+res.id+'")');
				
				$(".success").removeClass("success");
				$(tpl_tr).attr('class','success');
				$('#tpl_tr').after($(tpl_tr));
				$("#no_result").remove();
			}else if(res.status == "ok_update"){
				hideModal('add_member_key');
				$("#form-msg").html("<div class='alert alert-success fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
				
				var tpl_tr = $("#"+res.id);
				$(tpl_tr).find('.title').html(res.param_1);
				$(tpl_tr).find('.uuid_check').val(res.id);
				$(tpl_tr).find('.protocol').html(res.param_2);
				$(tpl_tr).find('.host').html(res.param_3);
				$(tpl_tr).find('.port').html(res.param_4);
				$(tpl_tr).find('.login').html(res.param_5);
				$(tpl_tr).find('.category').html(res.category_title);
				$(tpl_tr).find('.category').html(res.category_title);
				$(tpl_tr).find('.id-edit').attr('onclick','editKey("'+res.id+'","'+member_uuid+'")');
				$(tpl_tr).find('.id-delete').attr('onclick','delete("'+res.id+'")');
				
				$(".success").removeClass("success");
				$(tpl_tr).attr('class','success');
			}
		}
	});
}
function add_admin(){
	var login = $("#form-add-member-input-login").attr('value');
	var email = $("#form-add-member-input-email").attr('value');
	var pass = $("#pass").attr('value');
	var uuid = $("#form-input-uuid").attr('value');
	$.ajax({				
		type: "POST",
		async:false,
		url: "/admin/index.php?mod=admin&act=ajax_add_admin",
		data: "data[login]="+login+"&data[email]="+email+"&data[pass]="+pass+"&data[uuid]="+uuid,
		dataType: "json",
		success: function (res) {
			$("#form-error").html('');
			if(res.status == "error"){
				$("#form-error").html("<div class='alert alert-error fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
			}else if(res.status == "ok_update"){
				hideModal('add_member');
				$("#form-msg").html("<div class='alert alert-success fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
				$(".success").removeClass("success");
				$("#"+res.uuid).attr('class','success');
				$("#"+res.uuid).find(".login").html(res.login);
				$("#"+res.uuid).find(".email").html(res.email);
			}else if(res.status == "ok"){
				hideModal('add_member');
				$("#form-msg").html("<div class='alert alert-success fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");

				var tpl_tr = $('#tpl_tr').clone();
				$(tpl_tr).attr('id',res.uuid);
				$(tpl_tr).find('.login').html(res.login);
				$(tpl_tr).find('.email').html(res.email);
				$(tpl_tr).find('.id-block').attr('id',"block-"+res.uuid).attr('onclick',"memberBlock('"+res.uuid+"');");
				$(tpl_tr).find('.id-status').attr('id',"status-"+res.uuid);
				$(".success").removeClass("success");
				$(tpl_tr).attr('class','success');
				$('#tpl_tr').after($(tpl_tr));
			}
		}
	});
}
function add_category(){
	var title = $("#form-add-category-input-title").attr('value');
	var uuid = $("#form-input-uuid").attr('value');
	$.ajax({
		type: "POST",
		async:false,
		url: "/admin/index.php?mod=category&act=ajax_add_category",
		data: "data[title]="+title+"&data[id]="+uuid,
		dataType: "json",
		success: function (res) {
			$("#form-error").html('');
			if(res.status == "error"){
				$("#form-error").html("<div class='alert alert-error fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
			}else if(res.status == "ok_update"){
				hideModal('add_category');
				$("#form-msg").html("<div class='alert alert-success fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");
				$(".success").removeClass("success");
				$("#"+res.uuid).attr('class','success');
				$('#'+res.uuid+" .title").html(res.title);
			}else if(res.status == "ok"){
				hideModal('add_category');
				$("#form-msg").html("<div class='alert alert-success fade in text-left'><button type='button' class='close' data-dismiss='alert'>×</button>"+res.msg+"</div>");

				var tpl_tr = $('#tpl_tr').clone();
				$(tpl_tr).attr('id',res.id);
				$(tpl_tr).find('.title').html(res.title);
				$(tpl_tr).find('.id-inactive').attr('id',"inactive-"+res.id).attr('onclick',"memberBlock('"+res.id+"');");
				$(tpl_tr).find('.id-status').attr('id',"status-"+res.uuid);
				$(".success").removeClass("success");
				$(tpl_tr).attr('class','success');
				$('#tpl_tr').after($(tpl_tr));
			}
		}
	});
}
function deleteMember(uuid){
	if(sure()){
		$("#"+uuid).remove();
		$cnt = $(".id-delete").size()-1;
		if($cnt < 1){window.location.reload();}
		ajax_action('member','ajax_member_action','delete',uuid);
	}
}
function hideModal(id_modal){
	$('#'+id_modal).find("[type=text]").each(function(){
		$(this).val("");
		$(this).removeClass('error');
	});
	$('#'+id_modal).find("textarea").each(function(){
		$(this).val("");
		$(this).removeClass('error');
	});
	$('#'+id_modal).find("[type=password]").each(function(){
		$(this).val("");
		$(this).removeClass('error');
	});
	$('#'+id_modal).find("label.error").each(function(){
		$(this).hide();
	});
	$("#"+id_modal).modal("hide");
	$("#form-error").html("");
	$("#form-input-uuid").attr("value","");
}
function editMember(uuid){
	$('#add_member').modal();
	
	$.ajax({				
		type: "POST",
		async:false,
		url: "/admin/index.php?mod=member&act=ajax_info_member",			
		data: "uuid="+uuid,
		dataType: "json",									
		success: function (res) {
			$("#form-input-uuid").attr('value',res.uuid);
			$("#form-add-member-input-login").attr('value',res.login);
			$("#form-add-member-input-email").attr('value',res.email);
			$("#pass").attr('value',"<?=_cfg('input.hide.pass');?>");
			$("#form-add-member-input-cpass").attr('value',"<?=_cfg('input.hide.pass');?>");
		}
	});
	$('#myModalLabel').html('Edit member');
}
function editKey(uuid,member_uuid){
	
	$.ajax({				
		type: "POST",
		async:false,
		url: "/admin/index.php?mod=keys&act=ajax_info_member_key",			
		data: {"uuid":uuid,"member_uuid":member_uuid},
		dataType: "json",									
		success: function (res) {
			$("#form-add-member_key-input-category").val(res.category_id);
			$("#form-add-member_key-input-title").val(res.param_1);
			$("#form-add-member-key-input-protocol").val(res.param_2);
			$("#form-add-member-key-input-host").val(res.param_3);
			$("#form-add-member-key-input-port").val(res.param_4);
			$("#form-add-member-key-input-login").val(res.param_5);
			$("#form-add-member-input-password").val(res.param_6);
			$("#form-add-member-input-comment").val(res.param_7);
			$("#form-input-uuid").val(res.id);
			$('#add_member_key').modal();
		}
	});
	$('#myModalLabel').html('Edit key');
}
function categoryEdit(id){
	$('#add_category').modal();
	$('#form-add-category-input-title').val($('#'+id+" .title").html());
	$('#form-input-uuid').val(id);
	$('#myModalLabel').html('Edit category');
}
$(function(){
	$("#btn_add_member").click(function(){
		$('#add_member').modal();
		$('#myModalLabel').html('Add member');
	});
	$("#btn_add_member_key").click(function(){
		$('#add_member_key').modal();
		$('#myModalLabel').html('Add key');
	});
	$("#btn_add_category").click(function(){
		$('#add_category').modal();
		$('#myModalLabel').html('Add category');
	});
	$("#btn_add_admin").click(function(){
		$('#add_member').modal();
		$('#myModalLabel').html('Add admin');
	});
	$('#add_member').validate({
		rules: {				
			'login': {
				required: true,
				minlength: 8,
					maxlength: 16,
			},
			'email': {
				required: true,
				email:true,
			},
			'pass': {
				required: true,
				minlength: 8,
				maxlength: 16,
			},
			'cpass': {
				required: true,
				equalTo: "#pass",
				minlength: 8,
				maxlength: 16,
			}
		}
	});
	
	$('#add_member_key').validate({
		rules: {				
			'title': {
				required: true,
			},				
			'login': {
				required: true,
			},			
			'host': {
				required: true,
			},
			'password': {
				required: true,
			}
		}
	});
});